// pages/myCoupons/myCoupons.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    choo_modal:"none",
    radioValue: 1,
    checked: true,
    pType:0,
    type:0,
    type1:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight,
      type1:options.type
    });
    this.setData({
      balance: wx.getStorageSync("balance")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.request({
      url: app.globalData.url +'getUserOrderList',
      method: 'GET',
      data:{
        userId:wx.getStorageSync("userId"),
        type : that.data.type1,
        way:that.data.pType
      },
      success: function (res) {
        if (that.data.type == 4){
          that.setData({
            orderPasList: res.data.data
          });
        }else{
          that.setData({
            orderList: res.data.data
          });
        }
      },
    });
  },
  // 点击tab切换view
  city: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if (this.data.type === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        type: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + 'getUserOrderList',
      method: 'GET',
      data: {
        userId:wx.getStorageSync("userId"),
        type: that.data.type,
        way: that.data.pType
      },
      success: function (res) {
        if (that.data.type == 4){
          that.setData({
            orderPasList: res.data.data
          })
        }else{
          that.setData({
            orderList: res.data.data
          });
        }
      },
    });
  },
  // 点击tab切换view
  city1: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if (this.data.type1 === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        type1: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + 'getUserOrderList',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type1,
        way: this.data.pType
      },
      success: function (res) {
        if (that.data.type1 == 4) {
          that.setData({
            orderPasList: res.data.data
          })
        } else {
          that.setData({
            orderList: res.data.data
          });
        }
      },
    });
  },
  pType: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    console.info(e.target.dataset.type);
    if (this.data.pType === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        pType: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + 'getUserOrderList',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type1,
        way: this.data.pType
      },
      success: function (res) {
        if (that.data.type == 4) {
          that.setData({
            orderPasList: res.data.data
          })
        } else {
          that.setData({
            orderList: res.data.data
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onReady();
    var that = this;
    that.setData({
      token: wx.getStorageSync("token")
    });
    wx.request({
      url: app.globalData.url + "getUserByWechatUnoinId",
      method: "GET",
      data: {
        wechatUnoinId: wx.getStorageSync("wechatUnoinId"),
        token: that.data.token
      },
      success: function (res) {
        var data = res.data;
        if (data.result == 1) {
          wx.setStorageSync("balance", data.data.balance.toFixed(2));
          var balance = wx.getStorageSync("balance");
          that.setData({
            balance: balance,
          })
        } else {
          wx.reLaunch({
            url: '/pages/zcuslist/index?partnerUserId=' + that.data.partnerUserId,
          })
        }
      }
    });
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    this.data.pType = currPage.data.pType
    this.setData({
      pType: this.data.pType
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  goLogistice : function(e) {
      wx.navigateTo({
        url: '/pages/logistics/logistics?expressCompanyCode=' + this.data.orderPasList[e.currentTarget.dataset.index].orderProduct.expressCompanyCode + '&trackingNumber=' + this.data.orderPasList[e.currentTarget.dataset.index].orderProduct.trackingNumber + '&expressCompany=' + this.data.orderPasList[e.currentTarget.dataset.index].orderProduct.expressCompany + '&status=' + this.data.orderPasList[e.currentTarget.dataset.index].orderProduct.status
      })
    
  },
  trueGet: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否已经收到货物',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url + 'orderSigned',
            method: 'GET',
            data: {
              orderId: e.target.dataset.ordid,
              userId:  wx.getStorageSync("userId")
            },
            success: function (res) {
              wx.showToast({
                title: '收货成功！',  // 标题
                icon: 'success',   // 图标类型，默认success
                duration: 1500   // 提示窗停留时间，默认1500ms
              })
              that.onReady();
            }
          });
        }
      }
    });
  },
  goOrderDetail: function (e) {
    var that = this;
    if (e.currentTarget.dataset.communitygroup != 1) {
      var communitygroup=0;
    } else {
      var communitygroup = 1;
    }
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?orderId=' + e.currentTarget.dataset.ordid + "&type=" + e.currentTarget.dataset.type + '&communitygroup=' + communitygroup + '&pType=' + that.data.pType
    })
  },
  delOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要取消吗？',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用取消订单的方法了
          wx.request({
            url: app.globalData.url + 'delOrder',
            method: 'GET',
            data: {
              orderId: e.target.dataset.ordid,
              userId: wx.getStorageSync("userId")
            },
            success: function (res) {
              wx.showToast({
                title: "取消成功",
                icon: 'none',
                duration: 1500 
              })
              that.onReady();
            }
          });
        }
      }
    });
  },
  goPay: function (e) {
    var that = this;
    if (that.data.radioValue == 2) {
      var banlance = true;
    } else {
      var banlance = false;
    }
    var orderprice = e.target.dataset.orderprice
    wx.navigateTo({
      url: '/pages/cashier/cashier?myOrder=1&orderId=' + e.target.dataset.ordid + '&price1=' + e.target.dataset.orderprice
    })
      // wx.request({
      //   url: app.globalData.url + 'getOrderWeixinPrepayId',
      //   data: {
      //     userId: wx.getStorageSync("userId"),
      //     orderId: e.target.dataset.ordid,
      //     orderNumber: e.target.dataset.num,
      //   },
      //   method: "POST",
      //   header: {
      //     'content-type': 'application/x-www-form-urlencoded'
      //   },
      //   success: function (res) {
      //     var resultData = res.data;
      //     var hisId = resultData.hisId;
      //     if (res.data.result != 1 && res.data.result != 9) {
      //       wx.showToast({
      //         title: "数据异常，请稍后重试",
      //         icon: 'none'
      //       })
      //       return;
      //     }
      //     wx.requestPayment({
      //       'timeStamp': resultData.timeStamp,
      //       'nonceStr': resultData.nonceString,
      //       'package': resultData.packageStr,
      //       'signType': resultData.signType,
      //       'paySign': resultData.sign,
      //       //支付成功
      //       success: function (res) {
      //         if ("requestPayment:ok" === res.errMsg) {
      //           wx.navigateTo({
      //             url: '/pages/paySeccess/paySeccess?orderId=' + e.target.dataset.ordid + '&back=1'
      //           })
                
      //         }
      //       },
      //       //支付或者取消失败
      //       fail: function (res) {
      //         if ("requestPayment:fail cancel" === res.errMsg) {
                
      //           wx.showToast({
      //             title: "取消支付",
      //             icon: 'none'
      //           })

      //         } else {
      //           wx.showToast({
      //             title: "支付失败",
      //             icon: 'none'
      //           })

      //         }

      //       }
      //     })
      //   }
      // })
  },

  // 弹出
  modal_s: function (e) {
    this.setData({
      choo_modal: "block",
      z: true,
      ordId:e.target.dataset.ordid
    });
  },
  //消失
  modal_n: function () {
    this.setData({
      choo_modal: "none",
      z: false
    });
  },
  radio: function (e) {
    var that = this;
    var radioValue = e.currentTarget.dataset.value;
    that.setData({
      radioValue: radioValue
    })
    console.info(radioValue);
    if (radioValue == 1) {
      that.setData({
        checked1: false,
        checked: true,
        ordid: e.currentTarget.dataset.ordid
      })
    }
    if (radioValue == 2) {
      that.setData({
        checked1: true,
        checked: false,
        ordid: e.currentTarget.dataset.ordid
      })
    }
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  evaluation:function(e){
    wx.navigateTo({
      url: '/pages/userEvaluation/userEvaluation?productid=' + e.currentTarget.dataset.productid + '&opid=' + e.currentTarget.dataset.opid
    })
  },
  goExpressList: function (e) {
    wx.navigateTo({
      url: '/pages/expressList/expressList?orderId=' + e.currentTarget.dataset.ordid + "&type=" + e.currentTarget.dataset.type
    })
  },
  //联系团友
  phone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
})